<?php
namespace frontend\controllers;

use Yii;
use zhongkefu\zhongkefu;
use yii\web\Controller;
use yii\base\ErrorException;
use yii\web\NotFoundHttpException;
use Omnipay\Omnipay;
use yii\filters\AccessControl;

class WechatpayController extends Controller
{

    public $enableCsrfValidation = false;

    public function actionIndex(){
        //gateways: WechatPay_App, WechatPay_Native, WechatPay_Js, WechatPay_Pos
        $gateway    = Omnipay::create('WechatPay_App');
        $gateway->setAppId($config['app_id']);
        $gateway->setMchId($config['mch_id']);
        $gateway->setApiKey($config['api_key']);

        $order = [
            'body'              => 'The test order',
            'out_trade_no'      => date('YmdHis').mt_rand(1000, 9999),
            'total_fee'         => 1, //=0.01
            'spbill_create_ip'  => 'ip_address',
            'fee_type'          => 'CNY'
        ];

        /**
        * @var Omnipay\WechatPay\Message\CreateOrderRequest $request
        * @var Omnipay\WechatPay\Message\CreateOrderResponse $response
        */
        $request  = $gateway->purchase($order);
        $response = $request->send();

        //available methods
        $response->isSuccessful();
        $response->getData(); //For debug
        $response->getAppOrderData(); //For WechatPay_App
        $response->getJsOrderData(); //For WechatPay_Js
        $response->getCodeUrl(); //For Native Trade Type
    }
    public function actionNotify(){

        $gateway    = Omnipay::create('WechatPay');
        $gateway->setAppId($config['app_id']);
        $gateway->setMchId($config['mch_id']);
        $gateway->setApiKey($config['api_key']);

        $response = $gateway->completePurchase([
            'request_params' => file_get_contents('php://input')
        ])->send();

        if ($response->isPaid()) {
            //pay success
            var_dump($response->getData());
        }else{
            //pay fail
        }

    }

}